package top.oylan.manager.service.impl;

import org.springframework.stereotype.Service;
import top.oylan.manager.entity.Role;
import top.oylan.manager.entity.RoleUser;
import top.oylan.manager.entity.example.RoleExample;
import top.oylan.manager.mapper.RoleMapper;
import top.oylan.manager.service.RoleService;
import top.oylan.manager.tools.Pager;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-02-24
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public long countByExample(RoleExample example) {
        return roleMapper.countByExample(example);
    }

    @Override
    public List<Role> selectByExample(RoleExample example) {
        return null;
    }

    @Override
    public List<Role> selectByPager(Pager pager) {

        RoleExample example = new RoleExample();
        RoleExample.Criteria createria = example.createCriteria();
        pager.setTotalRow((int) countByExample(example));
        example.setOffset((pager.getCurrentPage()-1) * pager.getEveyPageRow());
        example.setSize(pager.getEveyPageRow());

        return roleMapper.selectByExample(example);
    }

    @Override
    public Role selectById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(Role role) {
        return roleMapper.insertSelective(role);
    }

    @Override
    public int update(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int delete(Integer id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void saveUserRoles(int userId, List<Integer> roleIdList) {
        roleMapper.saveUserRoles(userId,roleIdList);
    }

    @Override
    public List<RoleUser> getUserRoles(Integer userId) {
        return roleMapper.getUserRoles(userId);
    }
}
